
package com.platform.modules.mall.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.MapUtils;
import com.platform.common.utils.Query;
import com.platform.modules.mall.dao.MallShopsCategoryDao;
import com.platform.modules.mall.entity.MallShopsCategoryEntity;
import com.platform.modules.mall.service.MallShopsCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 店铺商品分类Service实现类
 *
 * @author
 * @date 2019-07-03 16:23:39
 */
@Service("mallShopsCategoryService")
public class MallShopsCategoryServiceImpl extends ServiceImpl<MallShopsCategoryDao, MallShopsCategoryEntity> implements MallShopsCategoryService {

    @Override
    public List<Map<String,Object>> getAllData(Map<String, Object> params) {
        Object temp;
        List<Map<String,Object>> lsit = this.listMaps(new QueryWrapper<MallShopsCategoryEntity>()
                .select("id","name as text","PARENT_ID as parentId")
                .eq(ObjectUtil.isNotEmpty(temp = params.get("id")),"ID",temp)
                .orderByDesc("PARENT_ID","SORT"));
        Map<String, List<Map<String,Object>>> tempMap = MapUtils.dataFormat(lsit, "parentId",lsit.size());
        return MapUtils.format(tempMap.get("0"),tempMap,1,"subCategoryList","id");
    }

    @Override
    public List<MallShopsCategoryEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.ID");
        params.put("asc", false);
        Page<MallShopsCategoryEntity> page = new Query<MallShopsCategoryEntity>(params).getPage();
        return page.setRecords(baseMapper.selectMallShopsCategoryPage(page, params));
    }

    @Override
    public boolean add(MallShopsCategoryEntity mallShopsCategory) {
        if(mallShopsCategory.getLevel().equals(1)){
            mallShopsCategory.setParentId("0");
        }
        return this.save(mallShopsCategory);
    }

    @Override
    public boolean update(MallShopsCategoryEntity mallShopsCategory) {
        return this.updateById(mallShopsCategory);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }
}
